package com.yanyeori.framework.elasticjob.job;

import org.springframework.boot.context.event.ApplicationStartedEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;

/**
 * 项目启动自动注册job到zk
 *
 * @author chenkuan 2020/8/19
 */
public abstract class Starter implements ApplicationListener<ApplicationEvent> {

    @Override
    public void onApplicationEvent(ApplicationEvent event) {
        if (event instanceof ApplicationStartedEvent) {
            JobManage jobManage = initContext(((ApplicationStartedEvent) event).getApplicationContext());
            start(jobManage);
        }
    }

    protected abstract void start(JobManage jobManage);

    private JobManage initContext(ApplicationContext context) {
        return context.getBean(JobManage.class);
    }
}
